﻿/****************************************************************************
 * Tutorial: CDCase_Array Test Class: Testing the Array DataType
 * File: CDCase_Array.as
 * 
 * Description: This is a rough benchmark test to create 100,000 Objects,
 * 				search for a specific object with index 90,000 , and clean up
 * 				displaying both Time to complete and System Memory used
 *
 * @author:  Haelix @ Zombie Flambe 
 * @site:    http://www.zombieflambe.com
 * @email:   haelix@zombieflambe.com
 * @version: 1.0
 * @created: 1st January 2009
 ****************************************************************************/

package zombieflambe {
	import flash.utils.getTimer;
	import flash.display.Sprite;
	import flash.net.LocalConnection;
	import flash.system.System;
	
	public class CDCase_Array extends Sprite {
		// Get TotalMemory before most everything
		private var beginMem:Number = System.totalMemory;
		
		private var caseArr:Array;
		private const MAX_CDS:int = 100000;
		private const MY_CD_TITLE:String = "cdTitle_90000";

		public function CDCase_Array() {
						
			var startTime:Number 	,  endTime:Number 		, totalTime:Number;
			var endCreate:Number 	,  totalCreate:Number;
			var startSearch:Number ,  endSearch:Number	, totalSearch:Number;
			var fullMem:Number  ,  endMem:Number;
			
			startTime = getTimer();
			
			caseArr  = new Array();
			
			var myCD = new Object();
			myCD.cdTitle = MY_CD_TITLE;
			
			// Initialize all our cases
			var i:int;
			for( i = 0; i < MAX_CDS; i++ )  {
				var tmpCD:Object = new Object();
				tmpCD.cdTitle = "cdTitle_" + i;
				
				// This lets us hold onto the reference of the tmp cd
				if( tmpCD.cdTitle == myCD.cdTitle )  
					myCD = tmpCD;
					
				caseArr.push( tmpCD );
			}

			endCreate = getTimer();
			
			// Get TotalMemory After Adding 100,000 array
			fullMem = System.totalMemory;
			
			startSearch = getTimer();
			
			var result:int 		= searchArray ( myCD );
			
			endSearch 	= getTimer();			
			
			clean();
			
			endTime = getTimer();
			
			totalCreate = ( endCreate - startTime 	) / 1000;
			
			totalSearch = ( endSearch - startSearch ) / 1000;
			
			totalTime 	= ( endTime   - startTime   ) / 1000;		
			
			/**********************************
			 * Unsupported Way to Force GC
			 * 	 From Grant Skinner's Series on Resource Management
			 *   
			 *   
			 *   From:  http://www.gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html
			 *****************************/
			try {
			   new LocalConnection().connect('foo');
			   new LocalConnection().connect('foo');
			} catch (e:*) {}
			
			// Get TotalMemory After Forcing GC
			endMem = System.totalMemory;
			
/** ** RESULTS ** **/
			
			trace( "Array Create/Find/Clean Test | MaxCDs: "      + MAX_CDS );
			trace( "******************************************************" );
			trace( "*   Search Outcome, -1 if Not Found, at Loc: " + result );
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* CREATE TIME TOTALS                                  " );
			trace( "******************************************************" );
			trace( "* Start Create Time: " + startTime        				);
			trace( "* End   Create Time: " + endCreate        				);
			trace( "* "														);
			trace( "* Total Create Time: " + totalCreate        			);
			trace( "******************************************************" );
			trace( "* SEARCH TIME TOTALS                                  " );
			trace( "******************************************************" );
			trace( "* Start Search Time: " + startSearch        			);
			trace( "* End   Search Time: " + endSearch        				);
			trace( "* "														);
			trace( "* Total Search Time: " + totalSearch        			);
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* TIME TOTALS                                         " );
			trace( "******************************************************" );
			trace( "* Total Start Time  : " + startTime        				);
			trace( "* Total End Time    : " + endTime        				);
			trace( "* "														);
			trace( "* Total Time        : " + totalTime        				);
			trace( "******************************************************" );
			trace( "******************************************************" );
			trace( "* MEMORY TOTALS ( each size was  / 1024 )             " );
			trace( "******************************************************" );
			trace( "* Beginning Memory   : " + beginMem / 1024  			);
			trace( "* Full Memory        : " + fullMem  / 1024  			);
			trace( "* Ending Memory      : " + endMem   / 1024  			);
			trace( "* "														);
					// reuse variables
					i  = ( ( fullMem - beginMem ) / 1024  					);
					result = ( ( endMem  - beginMem ) / 1024  				);
			trace( "* Total While Full   : " + i        					);
			trace( "* Total After Empty  : " + result 	  					);
			
		}

		private function searchArray( myCD:Object ):int  {
			var i:int =  0; //MAX_CDS - 1;
			var titleFoundAt:int = -1;
			for( i; i < MAX_CDS; i++ )  {
				if( caseArr[ i ] == myCD )  {
					titleFoundAt = i;
					break;
				}
			}
			return titleFoundAt;
		}

		private function clean():void  {
			var i:int = MAX_CDS - 1;
			while( i >= 0  )  {
				caseArr.splice( i , 1 );
				i--;
			}
			i = 0;
			caseArr = null;
			
		}
	}
}